package com.example.carRental.service.impl;

import com.example.carRental.mapper.ChartererMapper;
import com.example.carRental.mapper.CustomerMapper;
import com.example.carRental.rabbitmqDealUtils.DealMessage;
import com.example.carRental.service.MessageService;
import com.github.qcloudsms.SmsSingleSender;
import com.github.qcloudsms.SmsSingleSenderResult;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @author 92307
 * @version 1.0
 * @time 2022/4/3
 * @Describe describe
 */

@Component
public class MessageImpl implements MessageService {
    /**
     * @Description: 监听order_SMS_queue队列。
     * @Author: lpf
     * @Date: 2022/4/10
     */
    private static final int appid = 1400661497;
    private static final String appKey = "ad84f6cf228d29f81a420bb45f8bb91d";
    private static final int templateId = 1362840;
    private static final String smsSign = "学习的俺的号公众号";


    /**
     * @Description: 监听order_SMS_queue队列。
     * @Author: lpf
     * @Date: 2022/4/10
     */
    @Resource
    private CustomerMapper customerMapper;

    @Override
    @RabbitListener(queues = "order_SMS_queue")
    @RabbitHandler
    public void sentMessageSMS(DealMessage message) {
        Integer targetId = message.getTargetId();
        String phone = customerMapper.getPhoneById(targetId);
        String[] params = {};
        try {
            SmsSingleSender smsSender = new SmsSingleSender(appid, appKey);
            SmsSingleSenderResult result = smsSender.sendWithParam("86",phone,
                    templateId, params,smsSign,"","");
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    /**
     * @Description: 监听order_email_queue队列。
     * @Author: lpf
     * @Date: 2022/4/10
     */
    @Autowired
    JavaMailSender javaMailSender;
    @Resource
    private ChartererMapper chartererMapper;

    @Override
    @RabbitListener(queues = "order_email_queue")
    @RabbitHandler
    public void sentMessageMail(DealMessage message) {
        Integer targetId = message.getTargetId();
        String mailAddress = chartererMapper.getMailById(targetId);
        SimpleMailMessage mail=new SimpleMailMessage();
        mail.setText("提醒：您的订单已经创建成功，可以联系用户进行取车");
        mail.setSubject("租车服务");
        mail.setTo(mailAddress);
        mail.setFrom("923078148@qq.com");
        javaMailSender.send(mail);
    }
}
